package springbook.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import springbook.user.domain.User;

public class UserDao {
	private Connection c;
	private IConnectionMaker connectionMaker;

	UserDao(IConnectionMaker connectionMaker){
		this.connectionMaker = connectionMaker;
	}

	private void initConnection() throws ClassNotFoundException, SQLException{
		this.c = connectionMaker.getConnection();
	}

	public void add(User user) throws ClassNotFoundException, SQLException {
		initConnection();

		PreparedStatement ps = c.prepareStatement(
			"INSERT INTO USERS(ID, NAME, PASSWORD) VALUES(?,?,?)");
		ps.setString(1, user.getId());
		ps.setString(2, user.getName());
		ps.setString(3, user.getPassword());

		ps.executeUpdate();

		ps.close();
		c.close();
	}

	public User get(String id) throws ClassNotFoundException, SQLException {
		initConnection();

		PreparedStatement ps = c
				.prepareStatement("SELECT * FROM USERS WHERE ID = ?");
		ps.setString(1, id);

		ResultSet rs = ps.executeQuery();
		rs.next();
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));

		rs.close();
		ps.close();
		c.close();

		return user;
	}

	public void delete(String id) throws ClassNotFoundException, SQLException{
		initConnection();

		PreparedStatement ps = c
				.prepareStatement("DELETE FROM USERS WHERE ID = ?");
		ps.setString(1, id);

		ps.executeUpdate();
	}
}
